package com.ccc3gc.servlet;

import com.ccc3gc.dao.UserDao;
import com.ccc3gc.dao.impl.UserDaoImpl;
import com.ccc3gc.entity.User;
import com.ccc3gc.util.UpUtils;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {
    UserDao userDao = new UserDaoImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SmartUpload su = UpUtils.getSmartUpload(getServletConfig(), req, resp);
        Request request = su.getRequest();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String fileName = su.getFiles().getFile(0).getFileName();
        User user = new User(username, password, fileName);
        userDao.insertUser(user);
        resp.sendRedirect("userPage");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
